// pages/detail/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    foodlist:{},
    food_fetched: false,
    rec_food: '',
    food_location: undefined,
    food_img: undefined,
    btnText:'点击选菜',
    MaxNUM: 0,
    food_id:undefined,
    food_price:undefined,
    fno: undefined,
    food_comment: [],
    isLogin: false,
    userInfo: undefined
  },

  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad(options) {

  // },
  //传参，并获取云端对应数据
  onLoad: function (options) {
    this.setData({
      userInfo:getApp().globalData.userInfo
    })
    // console.log(options.id)
    var foodid = options.id;
     const db = wx.cloud.database();
     db.collection('food').where({
       _id: foodid,                 //查询food集合中字段_id为fid的值
     })
       .get({
         success: res => {
           this.setData({
             foodlist: res.data[0],
             
              rec_food : res.data["0"].fname,
              food_location : res.data["0"].floc,
              food_img: "cloud://cloud1-9gdorijx26597ab7.636c-cloud1-9gdorijx26597ab7-1315072742/images/"+ res.data["0"].img,
              food_fetched: true,
              food_id:res.data["0"]._id,
              food_price:res.data["0"].price,
              fno:res.data["0"].fno
           })
           console.log("查询成功", res.data[0]);
           this.getComments();
         },
         fail(err) {
           console.log("查询失败", err);
         }
       })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  /* 获取所有评论 */
  getComments()
  {
    const db = wx.cloud.database();
    db.collection('comments').where({
      fno: this.data.fno
    }).get({
      success:
        res => {
          let empty = [];
          for (let i in res.data)
          {
            empty.push(
              {
                record: res.data[i],
                cindex: parseInt(i),
              });
          }
          this.setData({
            food_comment:empty
          })
          //console.log(this.data.food_comment);
        },
        fail: err=>{
          console.log(err)
        }
    })

  },
  /* 提交评论的按钮绑定的函数 */
  onSubmitPressed: function(e)
  {
    if(e.detail.value.user_comment.match(/^[ ]*$/))
    {
      console.log('字符串全为空');
      return ;
    }
    const db = wx.cloud.database();
    let comment_db = db.collection("comments");
    let date = new Date();
    comment_db.add({
      data:{
        fno:this.data.fno,
        content:e.detail.value.user_comment,
        cdate: date.toLocaleDateString(),
        avatarUrl: this.data.userInfo.avatarUrl,
        nickName: this.data.userInfo.nickName,
      },
      success: res=>
      {
        this.getComments();
        thjs.setData({
          cur_comment: ''
        })
        console.log("插入成功");
      },
      fail: res=>
      {
        console.log("插入失败");
      }
    })
  }

})



